'use strict'

import cryptoJs from 'crypto-js'
let keyOne ='abcdefgh12345678'
let ivOne='12345678abcdefgh'
let domain_data = "";

import axios from "axios";
export default {
  // 加密函數
  jiami (word) {
    // console.log(word)
    // console.log('秘钥长度为：', keyOne.length)
    let key = cryptoJs.enc.Utf8.parse(keyOne)
    let iv = cryptoJs.enc.Utf8.parse(ivOne)
    let enc = ''
    
    // if (typeof word === 'string') {
    //   enc = cryptoJs.AES.encrypt(word, key, {
    //     iv: iv,
    //     mode: cryptoJs.mode.CBC,
    //     padding: cryptoJs.pad.Pkcs7
    //   })
    // } else if (typeof word === 'object') {
    //   // let data = JSON.stringify(word)
    //   enc = cryptoJs.AES.encrypt(word, key, {
    //     iv: iv,
    //     mode: cryptoJs.mode.CBC,
    //     padding: cryptoJs.pad.Pkcs7
    //   })
    // }

    enc = cryptoJs.AES.encrypt(word, key, {
      iv: iv,
      mode: cryptoJs.mode.CBC,
      padding: cryptoJs.pad.Pkcs7
    })
    return enc.toString();

    let encResult = enc.ciphertext.toString()
    return encResult
  },

  // 解密函數
  jiemi (word) {
    // console.log('传入的密文：', word)
    let key = cryptoJs.enc.Utf8.parse(keyOne)
    let iv = cryptoJs.enc.Utf8.parse(ivOne)
    let dec = cryptoJs.AES.decrypt(word, key, {
      iv: iv,
      mode: cryptoJs.mode.CBC,
      padding: cryptoJs.pad.Pkcs7
    })
    let decData = cryptoJs.enc.Utf8.stringify(dec)
    return decData.toString();

    // let dec = cryptoJs.AES.decrypt(cryptoJs.format.Utf8.parse(word), key, {
    //   iv: iv,
    //   mode: cryptoJs.mode.CBC,
    //   padding: cryptoJs.pad.Pkcs7
    // })
    // let decData = cryptoJs.enc.Utf8.stringify(dec)
    // return decData
  },

  getDomain(){
    //首先获取加密域名
    
    let index=0;
    axios({
      url:"https://dom.ilucode.com/lumafenfa/domain/dom.txt",
      withCredentials:false,
    }).then(
      (response) => {
        domain_data = response["data"];
        
        this.getUseDomain(index);
      }
    ).catch(response =>{
      console.log('err',response);
    });
  },
  getUseDomain(index){
    // console.log(domain_data);
    let end_doman = this.jiemi(domain_data);
    // console.log(end_doman);
    end_doman = JSON.parse(end_doman);
  
    let yuan = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
    let jiami1 = ['o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n'];
    let jiami2 = ['k','l','m','n','w','x','y','z','a','b','c','d','e','o','p','q','r','s','t','u','v','f','g','h','i','j'];
    let jiami3 = ['n','s','t','u','v','f','g','h','i','j','k','w','x','y','z','a','b','c','d','e','o','p','q','r','l','m'];
    let jiami4 = ['m','n','g','h','i','j','k','w','x','s','t','u','v','f','y','z','a','b','c','d','e','o','p','q','r','l'];
    let jiami5 = ['t','u','l','m','n','v','f','y','z','a','b','c','d','e','o','p','q','r','g','h','i','j','k','w','x','s'];
    let jiami6 = ['d','e','o','p','q','r','g','h','i','j','k','w','x','s','t','u','l','m','n','v','f','y','z','a','b','c'];
    let jiami7 = ['f','y','z','a','b','c','d','e','o','p','q','r','g','h','i','j','k','w','x','s','t','u','l','m','n','v'];
    let jiami8 = ['r','g','h','i','j','k','w','x','s','t','u','l','m','n','v','f','y','z','a','b','c','d','e','o','p','q'];
    let jiami9 = ['e','o','p','q','r','g','h','i','j','k','w','x','s','t','u','l','m','n','v','f','y','z','a','b','c','d'];
    let jiami10 = ['j','k','w','x','s','t','u','l','m','n','v','f','y','z','a','b','c','d','e','o','p','q','r','g','h','i'];
    let jiami = [jiami1,jiami2,jiami3,jiami4,jiami5,jiami6,jiami7,jiami8,jiami9,jiami10];

    //解密域名
    let value = end_doman[index];
    let doman = value['a'];
    let randNum = value['b'];

    let jiamiArr = jiami[randNum];
    let arr = doman.split('|');
    let str = "";
    arr.forEach(value_in=>{
      if(value_in != '|'){
        let str_index = jiamiArr.indexOf(value_in);
        console.log(str_index);
        if(str_index>-1){
          str+=yuan[str_index];
        }else{
          str+=value_in;
        }
      }
    });
// console.log(str);
    axios({
      url:str+"/api/index/testApi",
      withCredentials:false,
    }).then(
      (response) => {
        if(response == 'api'){
          console.log(str);
          this.setCookie('domain', encodeURI(str),10000);
          return;
        }

        index++;
        this.getUseDomain(index);
      }
    ).catch(response =>{
      console.log('err',response);
      index++;
      this.getUseDomain(index);
    });
  },
  //获取cookie、
    getCookie(name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if (arr = document.cookie.match(reg))
   return (arr[2]);
  else
   return null;
 },
 //设置cookie,增加到vue实例方便全局调用
  setCookie (c_name, value, expiredays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
 },
}